<template>
  <div class="main-box">
    <el-table
      :data="memberList"
      border
      style="width: 100%"
    >
      <el-table-column
        fixed
        prop="userId"
        label="编号"
        align="center"
        width="100"
      />
      <el-table-column prop="userName" label="姓名" align="center" width="100" />
      <el-table-column prop="reason" label="入社理由" width="300" align="center" />
      <el-table-column label="性别" prop="userGender" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ genderList[scope.row.userGender] }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="clazzName" label="班级" width="300" align="center" />
      <el-table-column prop="userQq" label="QQ" align="center" width="100" />
      <el-table-column prop="userTelephone" label="电话" align="center" width="150" />
      <el-table-column prop="status" label="审核状态" align="center" width="200">
        <template slot-scope="scope">
          <el-tag type="danger"> 用户报名未通过审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            style="margin-left: 10%;"
            @click="handleApproval(scope.$index, scope.row)"
          >审批
          </el-button>
          <el-button
            size="mini"
            type="danger"
            style="margin-left: 10%;"
            @click="handleNo(scope.$index, scope.row)"
          >拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <div class="pagination-box">
      <el-pagination
        :page-size="pageSize"
        :current-page="pageNum"
        :total="total"
        :page-sizes="[5, 10, 20, 50, 200, 300, 400]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import localStorageUtil from '../../../utils/localStorageUtil'

export default {
  data() {
    return {
      memberList: [
        {
          userId: '',
          userName: '',
          userDepartment: '',
          userQq: '',
          userTelephone: '',
          userGender: '',
          clazzName: '',
          reason: ''
        }
      ],
      genderList: ['男', '女'],
      /* 默认每页显示条数*/
      pageSize: 5,
      /* 默认显示第一页数据*/
      pageNum: 1,
      /* 用户列表总记录条数*/
      total: 1
    }
  },
  created() {
    this.fetchMembers()
    this.fetchTotal()
  },
  methods: {
    /* 获取用户总记录条数*/
    fetchTotal() {
      const params = {
        status: 0,
        communityId: localStorageUtil.read('SET_USER').userCommunityId
      }
      this.$store.dispatch('user/getMemberTotal', params).then((res) => {
        this.total = res.data
      })
    },
    /* 每页显示多少条改变时间*/
    handleSizeChange(val) {
      this.pageNum = 1
      this.pageSize = val
      this.fetchUsers()
    },
    /* 页码发生改变时间*/
    handleCurrentChange(val) {
      this.pageNum = val
      this.fetchUsers()
    },
    fetchMembers() {
      const params = {
        status: 0,
        communityId: localStorageUtil.read('SET_USER').userCommunityId,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        order: 'desc'
      }
      this.$store.dispatch('user/members', params).then((res) => {
        this.memberList = res.data
      })
    },
    handleApproval(index, row) {
      const params = {
        communityId: localStorageUtil.read('SET_USER').userCommunityId,
        userId: row.userId
      }
      this.$confirm(`确认同意用户：${row.userId}:${row.userName} 加入您的社团吗？`)
        .then(_ => {
          this.$store.dispatch('user/memberApprovalById', params).then((res) => {
            if (res.code === 200) {
              this.$message({
                showClose: true,
                message: '已成功加入',
                type: 'success'
              })
              const newMessage = {
                messageContent: `恭喜你加入社团！快来和我们一起快乐玩耍吧~`,
                messageRecv: `${row.userId}`,
                messageSend: localStorageUtil.read('SET_USER').userId,
                messageStatus: 0,
                messageTime: new Date().getTime()
              }
              this.$store.dispatch('message/newMessage', newMessage).then((res) => {
                if (res.code === 200) {
                  this.$message({
                    type: 'success',
                    showClose: true,
                    message: '成功'
                  })
                }
              })
            }
            this.fetchMembers()
          })
        })
        .catch(_ => {
          this.$message({
            showClose: true,
            message: '已经取消'
          })
        })
    },
    handleNo(index, row) {
      const params = {
        communityId: localStorageUtil.read('SET_USER').userCommunityId,
        userId: row.userId
      }
      this.$confirm(`确认拒绝用户：${row.userId}:${row.userName} 加入您的社团吗？`)
        .then(_ => {
          this.$store.dispatch('user/memberNoById', params).then((res) => {
            if (res.code === 200) {
              this.$message({
                showClose: true,
                message: '已拒绝',
                type: 'success'
              })
              const newMessage = {
                messageContent: `很抱歉你未能通过社团审核，但是${row.userName},我们欢迎你的下次报名~`,
                messageRecv: `${row.userId}`,
                messageSend: localStorageUtil.read('SET_USER').userId,
                messageStatus: 0,
                messageTime: new Date().getTime()
              }
              this.$store.dispatch('message/newMessage', newMessage).then((res) => {
                if (res.code === 200) {
                  this.$message({
                    type: 'success',
                    showClose: true,
                    message: '成功'
                  })
                }
              })
            }
            this.fetchMembers()
          })
        })
        .catch(_ => {
          this.$message({
            showClose: true,
            message: '已经取消'
          })
        })
    }
  }
}
</script>

<style>
  .main-box {
    margin: 2% 2%;
  }
  .pagination-box {
    margin: 4% 30%;
  }
</style>
